﻿using System;
using System.Web;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.API.Logs.V1
{
    using LdCms.Common.Net;
    using LdCms.EF.DbModels;
    using LdCms.IBLL.Log;
    using LdCms.Model.Logs;
    using LdCms.Web.Services;

    /// <summary>
    /// 访问日志接口：
    ///     1、写入访问日志
    ///        URL：log/visitor/record/save?uuid=uuid
    ///        POST：{"host":"","absolute_uri":"","query_string":""}
    /// 
    /// </summary>
    [ApiVersion("1.0")]
    [ControllerName("log/visitor/record")]
    public class LogVisitorRecordController : BaseApiController
    {
        private readonly IBaseApiManager BaseApiManager;
        private readonly IVisitorRecordService VisitorRecordService;
        public LogVisitorRecordController(IBaseApiManager BaseApiManager, IVisitorRecordService VisitorRecordService) : base(BaseApiManager)
        {
            this.BaseApiManager = BaseApiManager;
            this.VisitorRecordService = VisitorRecordService;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="fromValue"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("save")]
        public IActionResult Save(string uuid, [FromBody]LogVisitorRecordSaveParams fromValue)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(uuid, fromValue);
                var verifyParams = IsSaveParams(fromValue);
                var entity = ToLogVisitorRecord(fromValue);
                var result = VisitorRecordService.SaveVisitorRecord(entity);
                if (result)
                    return Success(logId, "ok");
                else
                    return Error(logId, "fail");
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }

        #region 私有化方法
        private bool IsSaveParams(LogVisitorRecordSaveParams fromValue)
        {
            try
            {
                if (fromValue == null)
                    throw new Exception("params not empty！");
                if (string.IsNullOrEmpty(fromValue.host))
                    throw new Exception("lack host params！");
                if (string.IsNullOrEmpty(fromValue.absolute_uri))
                    throw new Exception("lack absolute_uri params！");
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private Ld_Log_VisitorRecord ToLogVisitorRecord(LogVisitorRecordSaveParams fromValue)
        {
            try
            {
                string companyId = InterfaceAccount.CompanyID;
                string ipAddress = Net.Ip;
                string host = fromValue.host;
                string absoluteUri = fromValue.absolute_uri;
                string queryString = fromValue.query_string;
                return new Ld_Log_VisitorRecord()
                {
                    SystemID = SystemID,
                    CompanyID = companyId,
                    ClientID = 1,
                    IpAddress = ipAddress,
                    Host = host,
                    AbsoluteUri = absoluteUri,
                    QueryString = HttpUtility.UrlDecode(queryString)
                };
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

    }
}